package com.mkb.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author meikb
 * @desc
 * @date 2024-04-01 10:15
 */
public class GeometryInBoundsChecker {
    // 判断点是否在矩形范围内
    private static boolean isPointInBounds(double x, double y, double north, double south, double east, double west) {
        return x >= west && x <= east && y >= south && y <= north;
    }

    // 判断圆形是否在矩形范围内
    private static boolean isCircleInBounds(double centerX, double centerY, double radius,
                                            double north, double south, double east, double west) {
        // 计算圆的外接矩形范围
        double circleNorth = centerY + radius;
        double circleSouth = centerY - radius;
        double circleEast = centerX + radius;
        double circleWest = centerX - radius;

        // 检查圆的外接矩形是否与地图视图范围相交
        if (circleNorth < south || circleSouth > north || circleEast < west || circleWest > east) {
            return false;
        }

        // 检查圆心是否在地图视图范围内
        return isPointInBounds(centerX, centerY, north, south, east, west);
    }

    // 判断多边形是否在矩形范围内
    private static boolean isPolygonInBounds(List<double[]> vertices,
                                             double north, double south, double east, double west) {
        // 检查多边形的每个顶点是否在地图视图范围内
        for (double[] vertex : vertices) {
            double x = vertex[1]; // 经度
            double y = vertex[0]; // 纬度
            if (!isPointInBounds(x, y, north, south, east, west)) {
                return false;
            }
        }
        return true;
    }

    // 统一方法，用于检查给定的几何形状是否在矩形范围内
    public static boolean isGeometryInBounds(String geometryType, Object geometry,
                                             double north, double south, double east, double west) {
        switch (geometryType.toLowerCase()) {
            case "point":
                // 点的逻辑处理，暂未实现
                break;
            case "line":
                // 线的逻辑处理，暂未实现
                break;
            case "polygon":
                // 多边形的逻辑处理
                @SuppressWarnings("unchecked")
                List<double[]> polygonVertices = (List<double[]>) geometry;
                return isPolygonInBounds(polygonVertices, north, south, east, west);
            case "circle":
                // 圆形的逻辑处理
                @SuppressWarnings("unchecked")
                Object[] circleData = (Object[]) geometry;
                double centerX = (double) circleData[0];
                double centerY = (double) circleData[1];
                double radius = (double) circleData[2];
                return isCircleInBounds(centerX, centerY, radius, north, south, east, west);
            default:
                System.out.println("Unsupported geometry type: " + geometryType);
        }
        return false;
    }

    public static List<double[]> convert(String polygonJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<List<Double>> polygonList = objectMapper.readValue(polygonJson, List.class);
            List<double[]> polygon = new ArrayList<>();
            for (List<Double> point : polygonList) {
                double lat = point.get(0);
                double lng = point.get(1);
                polygon.add(new double[]{lat, lng});
            }
            return polygon;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
